package org.test.dao.impl;

import org.test.dao.ClientDAO;
import org.test.domain.Client;
import org.test.domain.Order;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import java.sql.SQLException;
import java.util.*;
import java.util.List;

public class ClientDAOImpl implements ClientDAO {

    @Override
    public void create(Client client) throws SQLException {
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(client);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }

    @Override
    public void update(Long clientId, Client client) throws SQLException {
        Client client1;
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            client1 = (Client) em.find(Client.class, clientId);
            client1.setSecondName(client.getSecondName());
            client1.setAddress(client.getAddress());
            client1.setName(client.getName());
            client1.setSurname(client.getSurname());
            client1.setPhoneNumber(client.getPhoneNumber());
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }

    }

    @Override
    public Client read(Long clientId) throws SQLException {
        EntityManager em = null;
        Client client = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            client = (Client) em.find(Client.class, clientId);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return client;
    }

    @Override
    public List<Client> readAll() throws SQLException {
        EntityManager em = null;
        List<Client> clients = new ArrayList<Client>();
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            TypedQuery<Client> query = em.createQuery("SELECT client FROM Client client ", Client.class);
            clients = query.getResultList();
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return clients;
    }

    @Override
    public void delete(Long clientId) throws SQLException {
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            Client client = read(clientId);
            em.remove(client);
            em.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
    }

    @Override
    public List<Order> getOrdersOfClient(Long clientId) throws SQLException {
        List<Order> orders = new ArrayList<Order>();
        Client client;
        EntityManager em = null;
        try {
            EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
            em = emf.createEntityManager();
            em.getTransaction().begin();
            client = (Client) em.find(Client.class, clientId);
            orders = client.getClientOrders();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (em != null && em.isOpen())
                em.close();
        }
        return orders;
    }
}
